package com.gfdd.tea.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.gfdd.tea.pojo.AddressBook;
import com.gfdd.tea.pojo.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserMapper extends BaseMapper<User> {
    @Select("select * from user where openid = #{openid}")
    List<User> selUserById(@Param("id") String openid);

    @Insert("insert into user (openid, avatar_url, nick_name, first_login_time, last_login_time)" +
            " values (#{openid}, #{avatarUrl}, #{nickName}, #{first_login_time}, #{last_login_time} );")
    int insUser(User user);

    @Update("update user set openId = #{openId}, avatar_url = #{avatar_url}, nick_name = #{nick_name}, " +
            "last_login_time = #{last_login_time} where id = #{id} ;")
    int updByUID(User user);


    /**
     * 查询用户收货地址
     */
    @Select("select * from address_book where user_id = #{user_id} ")
    List<AddressBook> selAddress(@Param("user_id") int user_id);

    /**
     * 插入地址
     */
    @Insert("insert into address_book (user_id, consignee, phone, detail_address, create_time, update_time) " +
            "values (#{user_id}, #{consignee} ,#{phone}, #{detail_address}, #{create_time}, #{update_time} )")
    int insAddress(AddressBook addressBook);

    /**
     * 更新
     *
     */
    @Update("update address_book set consignee = #{consignee}, phone = #{phone}, detail_address = #{detail_address}, update_time = #{update_time} where id = #{id};")
    int updAddress(AddressBook addressBook);

    /**
     * 当设置默认地址的时候先将所有默认设为0
     */
    @Update("update address_book set is_default = 0 where user_id = #{user_id} ;")
    int updAddressIsDefaultToZero(@Param("user_id") int user_id);

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @Delete("delete from address_book where id = #{id} ")
    int delAddress(@Param("id") int id);
}
